<?php   
class DB{
  private $connection, $database, $query;
  public $debug;
  function __construct($debug){
    $this->connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
    $this->database = mysql_select_db(DB_DATABASE) or die(mysql_error());
    $this->debug = ($debug == true)?true:false;
    if($this->debug) echo 'Connection openned<hr>';
    $sql = 'SET NAMES utf8';
    if($this->debug) echo $sql.'<br>';
    $this->query = mysql_query($sql);
  }
  public function execute($sql){
    if($this->debug) echo $sql.'<br>';
    $this->query = mysql_query($sql) or die(mysql_error());
    while($row = mysql_fetch_assoc($this->query)){
      $return[] = $row;
    }
    if($this->debug) echo '<hr>';
    return $return;
  }
  public static function Encode($data,$pwd)
  {
      $pwd_length = strlen($pwd);
      for ($i = 0; $i < 255; $i++) {
          $key[$i] = ord(substr($pwd, ($i % $pwd_length)+1, 1));
          $counter[$i] = $i;
      }
      for ($i = 0; $i < 255; $i++) {
          $x = ($x + $counter[$i] + $key[$i]) % 256;
          $temp_swap = $counter[$i];
          $counter[$i] = $counter[$x];
          $counter[$x] = $temp_swap;
      }
      for ($i = 0; $i < strlen($data); $i++) {
          $a = ($a + 1) % 256;
          $j = ($j + $counter[$a]) % 256;
          $temp = $counter[$a];
          $counter[$a] = $counter[$j];
          $counter[$j] = $temp;
          $k = $counter[(($counter[$a] + $counter[$j]) % 256)];
          $Zcipher = ord(substr($data, $i, 1)) ^ $k;
          $Zcrypt .= chr($Zcipher);
      }
      return $Zcrypt;
  } 
  function __destruct() {
    mysql_free_result($this->query);
    mysql_close($this->connection);
    if($this->debug) echo 'Connection clossed<hr>';
  }
}
?>

